using System;
using System.IO;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Directory=Lucene.Net.Store.Directory;

namespace LuceneSearch.Indexer
{
    public class LuceneIndexWriterWrapper : IIndexWriter
    {
        private readonly IndexWriter indexWriter;

        public LuceneIndexWriterWrapper(Directory directory)
        {
            indexWriter = new IndexWriter(directory, new StandardAnalyzer(), true);
        }

        public void IndexFile(FileInfo info)
        {
            using (var stream = info.OpenRead())
            using (var reader = new StreamReader(stream))
            {
                var doc = new Document();

                doc.Add(new Field("path", info.FullName, Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("contents", reader));

                indexWriter.AddDocument(doc);
            }
        }

        public Directory GetDirectory()
        {
            return indexWriter.GetDirectory();
        }

        public int GetDocumentCount()
        {
            return indexWriter.DocCount();
        }

        public void AddIndexes(Directory[] dirs)
        {
            indexWriter.AddIndexes(dirs);
        }

        public void Optimize()
        {
            indexWriter.Optimize();
        }

        public void Close()
        {
            indexWriter.Close();
        }
    }
}